package com.it.myd.service.main.module.world;

import com.it.myd.bean.box.world.Item;
import com.it.myd.bean.main.User;
import com.it.myd.help.ItemLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemServiceImpl implements ItemService{
    @Autowired
    private PlayerService playerService;

    private Map<String, List<Item>> userCreatItemList = new HashMap<>(); //制作列表
    private Map<String, List<Item>> userItemList = new HashMap<>(); //背包列表
    private Map<String, List<Item>> userUseItemList = new HashMap<>(); //装备使用列表

    @Override
    public void setuserUseItemList(Map<String, List<Item>> userUseItemList) {
        this.userUseItemList = userUseItemList;
    }
    @Override
    public Map<String, List<Item>> getuserUseItemList() {
        return userUseItemList;
    }
    @Override
    public void setuserCreatItemList(Map<String, List<Item>> userCreatItemList) {
        this.userCreatItemList = userCreatItemList;
    }
    @Override
    public Map<String, List<Item>> getuserCreatItemList() {
        return userCreatItemList;
    }

    @Override
    public void setuserItemList(Map<String, List<Item>> userItemList) {
        this.userItemList = userItemList;
    }
    @Override
    public Map<String, List<Item>> getuserItemList() {
        return userItemList;
    }

    @Override
    public void adduserCreatItemList(User user, List<Item> list) {
        boolean flag = false;
        List<Item> itemList = this.userCreatItemList.get(user.getUser_id());
        List<Item> addItemlist = new ArrayList<>();
        if(itemList != null){
            for (Item item: list) {
                boolean f = false;
                for (Item ite: itemList) {
                    if(item.getId().equals(ite.getId())){
                        ite.setCount(item.getCount()+ite.getCount());
                        f = true;
                        continue;
                    }
                }
                if(f){
                    continue;
                }else{
                    addItemlist.add(item);
                }
            }
            if(addItemlist.size() > 0){
                for (Item item: addItemlist) {
                    itemList.add(item);
                }
            }
        }else{
            this.userCreatItemList.put(String.valueOf(user.getUser_id()),list);
        }
    }

    @Override
    public void playerCreate(User user) {
        List<Item> itemList = this.userCreatItemList.get(String.valueOf(user.getUser_id()));
        if(itemList != null && itemList.size() > 0){
            Item item = itemList.get(0);
            item.setTime(item.getTime()+1);
            if(item.getNeedTime()<=item.getTime()){
                ItemLoader itemLoader = new ItemLoader();
                Item i = itemLoader.getItem(item.getId());
                adduserItem(user,i);
                item.setCount(item.getCount()-1);
                item.setTime(0);
            }
            if(item.getCount()<=0){
                itemList.remove(item);
            }
        }
        if(itemList!=null && itemList.size() == 0){
            playerService.changerState(user,1);
        }
        this.userCreatItemList.put(String.valueOf(user.getUser_id()),itemList);
    }

    @Override
    public void adduserItem(User user, Item item) {
        boolean flag = false;
        item.setState("1");
        item.setCount(1);
        item.createdItem();
        List<Item> itemList = this.userItemList.get(String.valueOf(user.getUser_id()));
        ItemLoader itemLoader = new ItemLoader();
        if(itemList != null){
            if(item.getIsStack().equals("1")){
                boolean f = false;
                for (Item ite: itemList) {
                    if(ite.getId().equals(item.getId())){
                        ite.setCount(ite.getCount()+1);
                        f = true;
                        break;
                    }
                }
                if(!f){
                    itemList.add(item);
                }
            }else{
                itemList.add(item);
            }
        }else{
            List<Item> additemlist = new ArrayList<>();
            additemlist.add(item);
            this.userItemList.put(String.valueOf(user.getUser_id()),additemlist);
        }
    }

    @Override
    public List<Item> getBaseItemList() {
        ItemLoader itemLoader = new ItemLoader();
        return itemLoader.getBaseItem();
    }

    public void changerItem(User user,Item olditem,Item newitem){
        List<Item> itemList = this.userItemList.get(String.valueOf(user.getUser_id()));
        if(olditem != null){
            boolean flag = false;
            for (Item item: itemList) {
                if(item.getCode().equals(olditem.getCode())){
                    if(item.getState().equals("2")){
                        item.setState("1");
                    }
                    flag = true;
                    break;
                }
            }
            if(!flag){
                System.out.println("未找到对应之前装备");
            }
        }
        if(newitem != null){
            boolean flag = false;
            for (Item item: itemList) {
                if(item.getCode().equals(newitem.getCode())){
                    item.setState("2");
                    flag = true;
                    break;
                }
            }
            if(!flag){
                System.out.println("未找到对应装备");
            }
        }
    }

    @Override
    public void adduserUseItem(User user, Item item) {
        boolean flag = false;
        Item oldItem = null;
        List<Item> userUseItemList = this.userUseItemList.get(String.valueOf(user.getUser_id()));
        item.setState("2");
        ItemLoader itemLoader = new ItemLoader();
        if(userUseItemList != null){
            for (Item ite: userUseItemList) {
                if(ite.getRegion().equals(item.getRegion())){
                    oldItem = ite;
                }
            }
            if(item.getIsStack().equals("1")){
                boolean f = false;
                for (Item ite: userUseItemList) {
                    if(ite.getId().equals(item.getId())){
                        ite.setCount(ite.getCount()+item.getCount());
                        f = true;
                        break;
                    }
                }
                if(!f){
                    userUseItemList.add(item);
                }
            }else{
                userUseItemList.add(item);
            }
            userUseItemList.remove(oldItem);
        }else{
            List<Item> additemlist = new ArrayList<>();
            additemlist.add(item);
            this.userUseItemList.put(String.valueOf(user.getUser_id()),additemlist);
        }
        changerItem(user,oldItem,item);
    }
    @Override
    public void removeuserUseItem(User user, String itemCode){
        boolean flag = false;
        Item oldItem = null;
        List<Item> userUseItemList = this.userUseItemList.get(String.valueOf(user.getUser_id()));
        if(userUseItemList != null){
            for (Item ite: userUseItemList) {
                if(ite.getCode().equals(itemCode)){
                    oldItem = ite;
                }
            }
            userUseItemList.remove(oldItem);
        }
        changerItem(user,oldItem,null);
    }

    @Override
    public Item getItemByCode(User user,String code){
        List<Item> userItemList = this.userItemList.get(String.valueOf(user.getUser_id()));
        for (Item item: userItemList) {
            if (item.getCode().equals(code)) {
                return item;
            }
        }
        System.out.println("code"+code);
        return null;
    }

    public void deleteItem(User user,String code){
        List<Item> userItemList = this.userItemList.get(String.valueOf(user.getUser_id()));
        List<Item> userUseItemList = this.userUseItemList.get(String.valueOf(user.getUser_id()));
        for (Item item: userUseItemList) {
            if (item.getCode().equals(code)) {
                changerItem(user,item,null);
                userUseItemList.remove(item);
                break;
            }
        }


        for (Item item: userItemList) {
            if (item.getCode().equals(code)) {
                userItemList.remove(item);
                break;
            }
        }
    }
}
